import { createTemplateFileApi } from "@/api/waybillManagement/customerServiceWaybill";
import { message } from "ant-design-vue";

export default function OperationHook(state: any) {
  const { visible, passData, formState, formRef, emit, getDataValue, type } = state;
  const onFish = () => {
    console.log("onFish");
    console.log(visible.value);
  };

  const onSubmit = () => {
    formRef.value.validate().then(() => {
      let params: any;

      if (passData.value.selectRows.length) {
        params = {
          bizExportTemplateId: formState.id,
          selectStatus: type.value,
          idList: passData.value.selectRows.map((t: any) => t.id)
        };
      } else {
        params = {
          ...passData.value.searchForm,
          bizExportTemplateId: formState.id,
          selectStatus: type.value
        };
      }

      createTemplateFileApi(params).then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("创建任务成功，请前往导出中心下载！");
          emit("submit");
        }
      });
    });
  };
  const resetForm = () => {
    formRef.value.resetFields();
    emit("update:visible", false);
    formState.fieldNames = undefined;
  };
  const changeTemplate = (e: string) => {
    const { exportName, fieldNames } = getDataValue.value.find((t: any) => t.id === e);
    if (exportName) {
      formState.exportFileName = exportName;
      formState.fieldNames = fieldNames.split(",");
    } else {
      formState.exportFileName = undefined;
      formState.fieldNames = undefined;
    }
  };
  return {
    onFish,
    resetForm,
    onSubmit,
    changeTemplate
  };
}
